package logic

import (
	"context"

	"user_rpc/internal/svc"
	"user_rpc/pb/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListLogic {
	return &GetUserListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserListLogic) GetUserList(in *user.ListRequest) (*user.ListResponse, error) {
	userList, total, pages, err := l.svcCtx.UserModel.FindList(l.ctx, in.Page, in.PageSize)
	if err != nil {
		l.Logger.Errorf("GetUserList failed: page=%d, page_size=%d, error: %v", in.Page, in.PageSize, err)
		return nil, err
	}

	var users []*user.UserInfo
	for _, u := range userList {
		users = append(users, &user.UserInfo{Id: u.Id, Name: u.Name, Password: u.Password})
	}
	return &user.ListResponse{Total: total, Pages: pages, Data: users}, nil
}
